#include<func.h>
int main(int argc,char*argv[])
{
    // ./test 192.168.213.128 1234 
    ARGS_CHECK(argc,3);
    int  sockfd = socket(AF_INET,SOCK_STREAM,0); //获取socket
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    //检查是否连接上
    ERROR_CHECK(ret,-1,"connect");
    fd_set rdset;
    char buf[4096] = {0};
    while(1)
    {
        bzero(buf,sizeof(buf));
        FD_ZERO(&rdset);
        FD_SET(STDIN_FILENO,&rdset);
        FD_SET(sockfd,&rdset);
        select(sockfd+1,&rdset,NULL,NULL,NULL);
        if(FD_ISSET(sockfd,&rdset))
        {
            //如果服务端发来消息
            ssize_t sret = recv(sockfd,buf,sizeof(buf),0);
            //如果发来的是 ctrl+d 0
            if(sret == 0)
            {
                printf("www buyaolikaiwo\n");
                break; 
            }
            printf("buf =%s\n",buf);
        }
        if(FD_ISSET(STDIN_FILENO,&rdset))
        {
            //如果自己发
            ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
            if(sret == 0)
            {
                send(sockfd,"nishigehaoren\n",15,0);
                break;
            }
            send(sockfd,buf,sret,0);
        }
    }
    close(sockfd);
    return 0;
}